﻿using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Windows.Shapes;
using System.Windows.Input;
using OpenDynDnsChanger.AdminApp.MVVM;
using System.Windows;
using System.ServiceProcess;
using System.Security.Permissions;
using OpenDynDnsChanger.Service;
using OpenDynDnsChanger.Cryption;
using System.Windows.Controls;

namespace OpenDynDnsChanger.AdminApp.ViewModel
{
    public class MainViewModel : ViewModelBase
    {

        public MainViewModel()
        {
            if(!DesignerProperties.GetIsInDesignMode(new Rectangle()))
            {
                ConfigHelper.Init();

                LoadCredentials();
                LoadConfiguration();

                RegisterCommands();
                LoadController();

            }
        }


        #region Credentials


        CryptionHelper cryptionHelper;

        /// <summary>
        /// Load the current Credentials from Registry.
        /// </summary>
        void LoadCredentials()
        {
            cryptionHelper = new CryptionHelper();

            Username = ConfigHelper.Username;
        }

        public string Username
        {
            get;
            set;
        }

        #endregion

        #region Configuration

        void LoadConfiguration()
        {
            UpdateTime = (int)ConfigHelper.UpdateTime;
        }

        private int _updateTime;
        public int UpdateTime
        {
            get { return _updateTime; }
            set { _updateTime = value; RaisePropertyChanged("UpdateTime"); }
        }


        public 

        #endregion

        #region Service Controller

        void LoadController()
        {
            _currentController = new ServiceController(OpenDynDnsChanger.Service.Consts.ServiceName);
            RaisePropertyChanged("CurrentController");
            UpdateCommandAvtStates();

        }

        private ServiceController _currentController;
        public ServiceController CurrentController
        {
            get
            {
                return _currentController;
            }
            set
            {
                _currentController = value;
            }
        }

        
        #endregion

        #region Command availible

        /// <summary>
        /// Update the start- and stopbutton on View.
        /// </summary>
        private void UpdateCommandAvtStates()
        {
            RaisePropertyChanged("CanServiceStart");
            RaisePropertyChanged("CanServiceStop");
        }

        public bool CanServiceStart
        {
            get
            {
                return CurrentController.Status == ServiceControllerStatus.Stopped;
            }
        }

        public bool CanServiceStop
        {
            get
            {
                return CurrentController.Status == ServiceControllerStatus.Running;
            }
        }

        #endregion

        #region Commands

        private void RegisterCommands()
        {
            ExitCommand = new RelayCommand<object>(OnExit);

            StartServiceCommand = new RelayCommand<object>(OnServiceStart);
            StopServiceCommand = new RelayCommand<object>(OnServiceStop);

            SaveCredentialsCommand = new RelayCommand<PasswordBox>(OnSaveCredentials);
            SaveConfigurationCommand = new RelayCommand<object>(OnSaveConfiguration);

        }

        public ICommand ExitCommand
        {
            get;
            private set;
        }
        /// <summary>
        /// Close the Application
        /// </summary>
        /// <param name="param"></param>
        private void OnExit(object param)
        {
            ((App)App.Current).Shutdown();
        }


        public ICommand StartServiceCommand
        {
            get;
            private set;
        }
        /// <summary>
        /// Start the Service
        /// </summary>
        /// <param name="param"></param>
        private void OnServiceStart(object param)
        {
            if (CurrentController == null) return;

            CurrentController.Start();

            UpdateCommandAvtStates();

            CurrentController.WaitForStatus(ServiceControllerStatus.Running, new TimeSpan(0, 0, 15));

            App.Current.RaiseServiceChanged();
            
            UpdateCommandAvtStates();
        }


        public ICommand StopServiceCommand
        {
            get;
            private set;
        }
        /// <summary>
        /// Stop the Service
        /// </summary>
        /// <param name="param"></param>
        private void OnServiceStop(object param)
        {
            if (CurrentController == null) return;

            CurrentController.Stop();

            UpdateCommandAvtStates();

            CurrentController.WaitForStatus(ServiceControllerStatus.Stopped, new TimeSpan(0, 0, 15));

            App.Current.RaiseServiceChanged();

            UpdateCommandAvtStates();
        }


        public ICommand SaveCredentialsCommand
        {
            get;
            private set;
        }
        /// <summary>
        /// Save the current Credentials
        /// </summary>
        /// <param name="passwordBox">Passwortbox that contains the password</param>
        private void OnSaveCredentials(PasswordBox passwordBox)
        {
            ConfigHelper.Username = Username;
            cryptionHelper.Password = passwordBox.Password;
        }


        public ICommand SaveConfigurationCommand
        {
            get;
            private set;
        }
        /// <summary>
        /// Save the current Configuration
        /// </summary>
        /// <param name="param"></param>
        private void OnSaveConfiguration(object param)
        {

            ConfigHelper.UpdateTime = UpdateTime;

        }


        #endregion



    }
}
